<template>
  <el-container class="home-container">
    <el-header>
      <div>
        <img src="../assets/logo.png" alt="" />
        <span>电商管理项目</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <el-container>
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <!-- 侧边栏界面 -->
        <div class="toggle-button" @click="toggleCollapse">
          |||
        </div>
        <el-menu
          :collapse-transition="false"
          background-color="#373d41"
          text-color="#fff"
          active-text-color="#ffd04b"
          unique-opened
          :collapse="isCollapse"
          :router="true"
          :default-active="activePath"
        >
          <el-submenu :index="item.id + ''">
            <template slot="title">
              <i
                class="el-icon-location"
                v-for="item in menulist"
                :key="item.id"
              ></i>
              <span>{{ item.authName }}</span>
            </template>

            <el-menu-item
              @click="savaNavState('/' + item.path)"
              :index="'/' + item.path"
              v-for="item in item.children"
              :key="item.id"
            >
              <i class="el-icon-location"></i>
              <span>二级菜单</span>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      menulist: [],
      isCollapse: true,
      //被激活的高亮显示标签
      activePath: "",
    };
  },
  created() {
    this.getMuenList();
    this.activePath = window.sessionStorage.getItem("activePath");
  },
  methods: {
    logout() {
      window.sessionStorage.clear();
      this.$router.push("./Login");
    },
    togglrCollapse() {
      this.isCollapse = !this.isCollapse;
    },
    savaNavState(avtivePath) {
      window.sessionStorage.setItem("activePath", avtivePath);
      this.avtivePath = activePath;
    },
  },
  async getMuenList() {
    const { data: res } = await this.$http.get("menus");
    if (res.meta.status !== 200) return this.$message.error(res.meta.msg);
    this.menulist = res.data;
  },
};
</script>

<style lang="less" scoped>
.el-container {
  height: 100%;

  .el-header {
    display: flex;
    justify-content: space-between;
    background-color: #373d41;
    padding-left: 0px;
    align-items: center;
    color: white;
    > div {
      font-size: 20px;
      display: flex;
      align-items: center;
      span {
        margin-left: 15px;
      }
    }
  }
  .el-aside {
    background-color: #373d41;
    .el-menu {
      border-right: none;
    }
    .toggle-button {
      background-color: #4a5064;
      font-size: 10px;
      line-height: 24px;
      color: #fff;
      text-align: center;
      letter-spacing: 0.2em;
      cursor: pointer;
    }
  }
  .el-main {
    background-color: #eaedf1;
  }
}
</style>
